package com.example.lushudao_admin.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 六星伊
 */
@RestController
public class RedisController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @RequestMapping("/setString")
    public String setString(@RequestBody JSONObject req){
        String user_acc=req.getString("name");
        String user_pwd=req.getString("passwd");

        try {
            redisTemplate.opsForValue().set(user_acc,user_pwd);
            return "DONE";
        } catch (Exception e) {
            return "Fail";
        }

    }
    @RequestMapping("/getString")
    public Object getString(@RequestBody JSONObject req){
        String user_acc=req.getString("name");
        try {
            String passwd = redisTemplate.opsForValue().get(user_acc);
            return "find success!"+passwd;
        } catch (Exception e) {
            return "find faild!";
        }
    }
    @RequestMapping("/updateString")
    public Object updateString(@RequestBody JSONObject req){
        String user_acc=req.getString("name");
        String user_pwd=req.getString("passwd");
        try {
            redisTemplate.opsForValue().set(user_acc,user_pwd);
            return "change success!";
        } catch (Exception e) {
            return "change success!";
        }
    }
    @RequestMapping("/delString")
    public Object delString(@RequestBody JSONObject req){
        String user_acc=req.getString("name");
        try {
            redisTemplate.delete(user_acc);
            return "delect success!";
        } catch (Exception e) {
            return "delect success!";
        }
    }

}
